﻿using GreedySnake.UI.View.ViewEx;

namespace GreedySnake.UI.View
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            snakeMap1.GameOverCallback += SnakeMap1_GameOverCallback;
            snakeMap1.SuccessCallback += SnakeMap1_SuccessCallback;
            snakeMap1.Interval = GetInterval(trackBar1.Value);
        }

        private void SnakeMap1_SuccessCallback(int obj)
        {
            label1.ExecBeginInvoke(() =>
            {
                label1.Text = obj.ToString();
            });
        }

        private void SnakeMap1_GameOverCallback()
        {
            MessageBox.Show("over");
        }
        bool _status = false;
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (_status)
            {
                snakeMap1.Stop();
            }
            else
                snakeMap1.Start();
            _status = !_status;
            btnStart.Text = _status ? "暂停" : "开始";
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (!snakeMap1.IsAI)
            {
                if (keyData == Keys.Up)
                {
                    snakeMap1.SnakeDirection = Direction.Top;
                    // 处理向上键的逻辑
                    return true; // 返回true表示已处理该键盘输入
                }
                else if (keyData == Keys.Down)
                {
                    // 处理向下键的逻辑
                    snakeMap1.SnakeDirection = Direction.Bottom;
                    return true;
                }
                else if (keyData == Keys.Left)
                {
                    // 处理向左键的逻辑
                    snakeMap1.SnakeDirection = Direction.Left;
                    return true;
                }
                else if (keyData == Keys.Right)
                {
                    // 处理向右键的逻辑
                    snakeMap1.SnakeDirection = Direction.Right;
                    return true;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            snakeMap1.Reset();
            _status = false;
            btnStart.Text = _status ? "暂停" : "开始";
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label2.Text = trackBar1.Value.ToString();
            snakeMap1.Interval = GetInterval(trackBar1.Value);
        }
        private int GetInterval(int val)
        {
            return val switch
            {
                1 => 300,
                2 => 250,
                3 => 200,
                4 => 150,
                5 => 100,
                6 => 80,
                7 => 70,
                8 => 50,
                9 => 30,
                _ => 10
            };
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            snakeMap1.IsAI = !radioButton1.Checked;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //snakeMap1.Dispose();

        }
    }
}
